﻿// Decompiled with JetBrains decompiler
// Type: Microsoft.InfoCards.ListCardsInFileRequest
// Assembly: infocard, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 8E14765A-6610-409A-BA36-099A0642905D
// Assembly location: E:\git\ALLIDA\windll\infocard.exe

using Microsoft.InfoCards.Diagnostics;
using System;
using System.Globalization;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Xml;
using System.Xml.Schema;

namespace Microsoft.InfoCards
{
  internal class ListCardsInFileRequest : UIAgentRequest
  {
    private InfoCardPolicy m_policy;
    private RoamingStoreFile m_roamingFile;
    private string m_filename;
    private string m_passphrase;

    public ListCardsInFileRequest(
      IntPtr rpcHandle,
      Stream inArgs,
      Stream outArgs,
      ClientUIRequest parent)
      : base(rpcHandle, inArgs, outArgs, parent)
    {
      this.m_roamingFile = new RoamingStoreFile();
    }

    protected override void OnInitializeAsSystem()
    {
      base.OnInitializeAsSystem();
      this.m_policy = this.GetPolicy();
    }

    protected override void OnMarshalInArgs()
    {
      BinaryReader reader = (BinaryReader) new InfoCardBinaryReader(this.InArgs, Encoding.Unicode);
      this.m_filename = Utility.DeserializeString(reader);
      this.m_passphrase = Utility.DeserializeString(reader);
    }

    protected override void OnProcess()
    {
      this.Cleanup(false, true);
      try
      {
        try
        {
          using (FileStream fileStream = new FileStream(this.m_filename, FileMode.Open, FileAccess.Read, FileShare.Read))
          {
            XmlReaderSettings defaultReaderSettings = InfoCardSchemas.CreateDefaultReaderSettings();
            defaultReaderSettings.IgnoreWhitespace = false;
            using (XmlReader reader = InfoCardSchemas.CreateReader((Stream) fileStream, defaultReaderSettings))
            {
              this.m_roamingFile.ReadFrom(this.m_passphrase, reader);
              this.ParentRequest.SetContext<RoamingStoreFile>(this.m_roamingFile);
            }
          }
        }
        catch (ImportException ex)
        {
          throw;
        }
        catch (CryptographicException ex)
        {
          throw InfoCardTrace.ThrowHelperError((Exception) new PasswordValidationException(SR.GetString("InvalidImportFile"), (Exception) ex));
        }
        catch (XmlSchemaValidationException ex)
        {
          throw InfoCardTrace.ThrowHelperError((Exception) new ImportException(SR.GetString("SchemaValidationFailed"), (Exception) ex));
        }
        catch (UnauthorizedAccessException ex)
        {
          throw InfoCardTrace.ThrowHelperError((Exception) new ImportException(SR.GetString("ImportInaccesibleFile"), (Exception) ex));
        }
        catch (IOException ex)
        {
          throw InfoCardTrace.ThrowHelperError((Exception) new ImportException(SR.GetString("InvalidImportFile"), (Exception) ex));
        }
        catch (XmlException ex)
        {
          throw InfoCardTrace.ThrowHelperError((Exception) new ImportException(SR.GetString("InvalidImportFile"), (Exception) ex));
        }
      }
      catch (ImportException ex)
      {
        this.Cleanup(false, true);
        throw InfoCardTrace.ThrowHelperError((Exception) new ImportStoreException(ex.Message));
      }
      finally
      {
        this.Cleanup(true, false);
      }
    }

    protected override void OnMarshalOutArgs()
    {
      Stream outArgs = this.OutArgs;
      BinaryWriter binaryWriter = new BinaryWriter(outArgs);
      StoreConnection connection = StoreConnection.GetConnection();
      try
      {
        if (this.m_roamingFile != null)
        {
          if (this.m_roamingFile.Cards.Count != 0)
          {
            try
            {
              binaryWriter.Write(this.m_roamingFile.Cards.Count);
              for (int index = 0; index < this.m_roamingFile.Cards.Count; ++index)
              {
                binaryWriter.Flush();
                this.m_roamingFile.Cards[index].AgentSerialize(outArgs, this.ParentRequest is GetTokenRequest, this.m_policy, connection, new CultureInfo(this.ParentRequest.UserLanguage));
              }
              return;
            }
            catch (Exception ex)
            {
              this.Cleanup(true, true);
              if (!InfoCardTrace.IsFatal(ex))
                throw InfoCardTrace.ThrowHelperError((Exception) new ImportStoreException(SR.GetString("InvalidImportFile"), ex));
              throw;
            }
          }
        }
        binaryWriter.Write(0);
      }
      finally
      {
        connection.Close();
      }
    }

    private void Cleanup(bool clearPwd, bool clearRoamingFile)
    {
      if (clearRoamingFile)
      {
        if (this.m_roamingFile != null)
          this.m_roamingFile.Clear();
        if (this.ParentRequest != null)
          this.ParentRequest.ClearContext<RoamingStoreFile>();
      }
      if (!clearPwd)
        return;
      this.m_passphrase = (string) null;
    }
  }
}
